package com.weavernorth.action.dataToMDM;

import com.weavernorth.humanresource.SalarRangeUtil;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.MyCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Iterator;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.action.dataToMDM
 * @date: 2020/9/11 17:38
 * @description: 【品牌】渠道闭店申请流程:编码数据传至SAP
 */
public class qdbdCodeToMDM implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        Logger log = Logger4jUtil.getLoggerByName("/action/qdbdCodeToMDM/");
        BaseBean bb = new BaseBean();
        //推送
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        log.info("渠道闭店申请流程数据传至SAP数据开始。。。");
        RecordSet rs = new RecordSet();
        RecordSet rs_new = new RecordSet();
        //请求id
        String strRequestid = requestInfo.getRequestid();
        log.info("流程requestid=" + strRequestid);
        String strTable = requestInfo.getRequestManager().getBillTableName();
        log.info("表名：" + strTable);
        String strSql = "select a.* from " + strTable + " a where a.requestid=" + strRequestid + "";
        log.info("执行查询语句：" + strSql);
        rs.executeQuery(strSql);
        String strghbm = "";
        while (rs.next()) {
            //获取当前流程的规划编码
            strghbm = Util.null2String(rs.getString("ghbm"));
            log.info("规划编码：" + strghbm);
        }
        //报文请求体
        StringBuffer requestXml = new StringBuffer();
        if (!"".equals(strghbm)) {
            //拼接请求体
            requestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:rfc:functions'>" +
                    "<soapenv:Header/><soapenv:Body><urn:ZF_IF089_CUSTOMER_CREATE_MDM>");
            //根据规划编码获取建模表数据
            String strDataSql = "SELECT a.mdmcode,a.dpmc,a.sapbm,a.bdfs,a.bdsj,a.dpzt,t1.prop420453 AS zhanghu " +
                    " FROM uf_md_lc_qdxkfxsj a  " +
                    "LEFT JOIN uf_mdm_lc_data t1 ON a.zhanghu=t1.code " +
                    " where  a.id='" + strghbm + "'";
            log.info("sql执行语句：" + strDataSql);
            rs_new.executeQuery(strDataSql);
            if (rs_new.getCounts() > 0) {
                while (rs_new.next()) {
                    String strmdmcode = Util.null2String(rs_new.getString("mdmcode"));
                    log.info("MDM编码：" + strmdmcode);
                    String strsapbm = Util.null2String(rs_new.getString("sapbm"));
                    log.info("sap编码(客户编号)：" + strsapbm);
                    String strZhanghuzu = Util.null2String(rs_new.getString("zhanghu"));
                    log.info("客户账户组：" + strZhanghuzu);
                    String strbdsj = Util.null2String(rs_new.getString("bdsj"));
                    log.info("闭店时间：" + strbdsj);
                    String strDpzt = Util.null2String(rs_new.getString("dpzt"));
                    log.info("店铺状态：" + strDpzt);
                    String strdpmc = Util.null2String(rs_new.getString("dpmc"));
                    log.info("客户名称：" + strdpmc);
                    //店铺状态--ZYLZD3
                    requestXml.append("<I_CUSTOMER>\n");
                    requestXml.append("<ZYLZD3>" + strDpzt + "</ZYLZD3>\n");
                    //SAP主编码--客户编号
                    requestXml.append("<KUNNR>" + strsapbm + "</KUNNR>\n");
                    //客户名称
                    requestXml.append("<NAME1>" + strdpmc + "</NAME1>");
                    //客户账户组
                    requestXml.append("<KTOKD>" + strZhanghuzu + "</KTOKD>\n");
                    //关店日期
                    requestXml.append("<ZCLOSE>" + strbdsj + "</ZCLOSE>\n");
                    //国家
                    requestXml.append("<LAND1>CN</LAND1>");
                    requestXml.append("</I_CUSTOMER>");
                    requestXml.append("<I_MSGID></I_MSGID>");
                    requestXml.append("<I_SENDR></I_SENDR>");
                }
                requestXml.append("</urn:ZF_IF089_CUSTOMER_CREATE_MDM>\n" +
                        "</soapenv:Body>" +
                        "</soapenv:Envelope>");
                log.info("推送的xml:" + requestXml.toString());
                //获取地址
//                String strSendUrl = "http://10.5.0.30:8770/bpm_mdm?wsdl";
                String strSendUrl = bb.getPropValue("dataToMdm", "sapUrl");
                String strUsername = bb.getPropValue("dataToMdm", "username");
                String strPassWord = bb.getPropValue("dataToMdm", "password");
                log.info("sap接口地址：" + strSendUrl);
                try {
                    log.info("执行发送请求：");
                    String responseXml = MyCommonUtils.sendPost(strSendUrl, requestXml.toString(), "utf-8", strUsername, strPassWord);
                    log.info("返回的xml:" + responseXml);
                    //返回标识
                    String strReturnStatus = "";
                    //返回信息
                    String strMessage = "";
                    Document doc = null;//解析返回值开始
                    doc = DocumentHelper.parseText(responseXml);
                    Iterator<Element> its2 = doc.selectNodes("//RTFLG").iterator();
                    while (its2.hasNext()) {
                        Element element = its2.next();
                        strReturnStatus = element.getText();
                        if (!"".equals(strReturnStatus)) {
                            break;
                        }
                    }
                    log.info("返回值:" + strReturnStatus);
                    //返回值失败时,则提示失败
                    if ("E".equals(strReturnStatus)) {
                        Iterator<Element> its3 = doc.selectNodes("//RTMSG").iterator();
                        while (its3.hasNext()) {
                            Element element2 = its3.next();
                            strMessage = element2.getText();
                            if (!"".equals(strMessage)) {
                                break;
                            }
                        }

                    }
                    if ("E".equals(strReturnStatus)) {
                        requestInfo.getRequestManager().setMessagecontent(strMessage);
                        return FAILURE_AND_CONTINUE;
                    } else if ("".equals(strReturnStatus) || !"S".equals(strReturnStatus)) {
                        requestInfo.getRequestManager().setMessagecontent("接口调用失败！");
                        return FAILURE_AND_CONTINUE;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    requestInfo.getRequestManager().setMessagecontent("程序异常：" + e.toString());
                    return FAILURE_AND_CONTINUE;
                }
            } else {
                requestInfo.getRequestManager().setMessagecontent("该规划编码下没有可传数据！");
                return FAILURE_AND_CONTINUE;
            }
            log.info("渠道闭店申请流程数据传至SAP数据结束。。。");
            return SUCCESS;
        } else {
            requestInfo.getRequestManager().setMessagecontent("规划编码不能为空！");
            return FAILURE_AND_CONTINUE;
        }


    }

    public String senToMDM(String strghbm, RecordSet rs_new, RequestInfo requestInfo, BaseBean bb,Logger log) {
        //报文请求体
        StringBuffer requestXml = new StringBuffer();
        //拼接请求体
        requestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:rec='http://www.example.org/receivebpm/'>" +
                "<soapenv:Header/>" + "<soapenv:Body>" + "<rec:receivebmp>" + " <DATAINFOS>" +
                "<PUUID>?</PUUID>");
        if (!"".equals(strghbm)) {
            //根据规划编码获取建模表数据
            String strDataSql = "SELECT a.mdmcode,a.sapbm,a.bdfs,a.bdsj,a.dpzt " +
                    " FROM uf_md_lc_qdxkfxsj a  where  a.id='" + strghbm + "'";
            log.info("sql执行语句：" + strDataSql);
            rs_new.executeQuery(strDataSql);
            while (rs_new.next()) {
                String strmdmcode = Util.null2String(rs_new.getString("mdmcode"));
                log.info("MDM编码：" + strmdmcode);
                String strsapbm = Util.null2String(rs_new.getString("sapbm"));
                log.info("sap编码：" + strsapbm);
                String strbdsj = Util.null2String(rs_new.getString("bdsj"));
                log.info("闭店时间：" + strbdsj);
                String strDpzt = Util.null2String(rs_new.getString("dpzt"));
                log.info("店铺状态：" + strDpzt);
                //闭店方式
//                requestXml.append("<DATAINFO><DESC19>1</DESC19>");
                //状态
                requestXml.append("<DATAINFO><DESC56>" + strDpzt + "</DESC56>");
                //SAP主编码
                requestXml.append("<DESC81>" + strsapbm + "</DESC81>");
                //闭店时间
                requestXml.append("<DESC31>" + strbdsj + "</+DESC31>");
                //MDM主编码
                requestXml.append("<CODE>" + strmdmcode + "</CODE>");
                requestXml.append("</DATAINFO>");
            }
        }
        requestXml.append("</DATAINFOS></rec:receivebmp>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>");
        log.info("推送的xml:" + requestXml.toString());
        //获取地址
        //MDM测试地址：http://10.10.8.17:8770/bpm_mdm
        //MDM正式地址：http://10.5.0.30:8770/bpm_mdm?wsdl
//        String strSendUrl = "http://10.5.0.30:8770/bpm_mdm?wsdl";
        String strSendUrl = bb.getPropValue("dataToMdm", "mdmUrl");
        log.info("mdm接口地址：" + strSendUrl);
        try {
            log.info("执行发送请求：");
            String responseXml = MyCommonUtils.sendPost(strSendUrl, requestXml.toString(), "utf-8");
            log.info("返回的xml:" + responseXml);
            //返回标识
            String strReturnStatus = "";
            //返回信息
            String strMessage = "";
            Document doc = null;//解析返回值开始
            doc = DocumentHelper.parseText(responseXml);
            Iterator<Element> its2 = doc.selectNodes("//STATUS").iterator();
            while (its2.hasNext()) {
                Element element = its2.next();
                strReturnStatus = element.getText();
                if (!"".equals(strReturnStatus)) {
                    break;
                }
            }
            log.info("返回值:" + strReturnStatus);
            //返回值失败时,则提示失败
            if ("E".equals(strReturnStatus)) {
                Iterator<Element> its3 = doc.selectNodes("//ERROTEXT").iterator();
                while (its3.hasNext()) {
                    Element element2 = its3.next();
                    strMessage = element2.getText();
                    if (!"".equals(strMessage)) {
                        break;
                    }
                }

            }
            if ("E".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                return FAILURE_AND_CONTINUE;
            } else if ("".equals(strReturnStatus) || !"S".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent("接口调用失败！");
                return FAILURE_AND_CONTINUE;
            }

        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("程序异常：" + e.toString());
            return FAILURE_AND_CONTINUE;
        }
        log.info("渠道闭店申请流程数据传至MDM数据结束。。。");
        return SUCCESS;
    }
}
